"""
Case Type   : cm_ctl可靠性
Case Name   : cm_ctl stop -n -D 故障测试-stop前环境变量异常
Create At   : 2025-03-1
Owner       : sungang14
Description :
    1: 修改env文件中的$gausshome路径为错误路径;
    vim [env文件]
    2: 执行cm_ctl stop -n命令;
    cm_ctl stop -n nodeid -d datadir
    3: 恢复env文件中的$gausshome路径;
    vim [env文件]
    4: 再次执行cm_ctl stop -n命令;
    cm_ctl stop -n nodeid -d datadir
    5: 启动节点;
    cm_ctl start -n nodeid
Expect      :
    1: 成功
    2: 失败
    3: 成功
    4: 成功
    5: 成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()

    def test_cm(self):
        text = '----step1:修改env文件中的$gausshome路径为错误路径; expect:修改成功----'
        self.log.info(text)
        chown_cmd = f"echo 'export GAUSSHOME={macro.GAUSSHOME_BIN_PATH}' >> {macro.DB_ENV_PATH} "
        self.log.info(chown_cmd)
        touch_msg = self.common.get_sh_result(self.pri_user, chown_cmd)
        self.log.info(touch_msg)
        self.assertEqual('', touch_msg, "执行失败:" + text)

        text = '----step2:执行cm_ctl stop -n -D datadir命令; expect:执行失败----'
        self.log.info(text)
        stop_cmd = f'source {macro.DB_ENV_PATH};' \
                   f'cm_ctl stop -n 2 -D {macro.DB_INSTANCE_PATH};'
        self.log.info(stop_cmd)
        stop_msg = self.common.get_sh_result(self.pri_user, stop_cmd)
        self.assertIn(f"cm_ctl: Fail to access the cluster static config file", stop_msg, "执行失败:" + text)

        text = '----step3:恢复env文件中的$gausshome路径,再次执行cm_ctl stop -n -D datadir命令; expect:执行停止----'
        self.log.info(text)
        sed_cmd = f"sed  -i '$d' {macro.DB_ENV_PATH};cat {macro.DB_ENV_PATH}"
        self.common.get_sh_result(self.pri_user, sed_cmd)
        time.sleep(15)
        self.log.info(stop_cmd)
        stop_msg1 = self.common.get_sh_result(self.pri_user, stop_cmd)
        self.log.info(stop_msg1)
        self.assertIn(self.constant.cm_stop_instance_success_msg, stop_msg1,
                      "执行失败:" + text)

        text = '----step4:查看集群状态; expect:集群状态异常，备节点停止----'
        self.log.info(text)
        time.sleep(5)
        show_status = f'source {macro.DB_ENV_PATH};cm_ctl query -Civ'
        self.log.info(show_status)
        status_msg = self.common.get_sh_result(self.pri_user, show_status)
        self.assertIn(self.constant.CLUSTER_DEGRADED_MSG, status_msg,
                      "执行失败:" + text)
        self.assertIn('6002     S Down    Manually stopped', status_msg,
                      "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '-----step5:启动节点; expect:成功-----'
        self.log.info(text)
        start_cmd = f"sed  -i '\|export GAUSSHOME={macro.GAUSSHOME_BIN_PATH}|d' {macro.DB_ENV_PATH};source {macro.DB_ENV_PATH};" \
                    f'cm_ctl start -n 2 -D {macro.DB_INSTANCE_PATH};'
        self.log.info(start_cmd)
        start_msg = self.common.get_sh_result(self.pri_user, start_cmd)
        self.assertIn(self.constant.cm_start_instance_success_msg, start_msg,
                      "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
